#!/usr/bin/env python2.7
# coding: utf8

"""
Задание: написать итератор, генерирующий все строки, которые являются
палиндромами
"""

import itertools

def all_naturals():
    n = 1
    while True:
        yield n
        n += 1

def all_chars():
    """
    Iterates over all lowercase alphanumeric characters
    """
    c = 0
    while True:
        yield '0'
        yield '1'
        break
        #if c <= ord('z') - ord('a'): # [0; 25]
        #    yield chr(c + ord('a'))
        #elif c in range(26, 36): # [26; 35]
        #    yield chr(c - (ord('z') - ord('a')) + ord('0') - 1)
        #else:
        #    break
        #c += 1

def gen_str():
    """
    Generates all possible alphanumeric ASCII strings
    """
    charmap = list(all_chars())
    return itertools.chain.from_iterable(itertools.product(charmap, repeat=num)
                                         for num in all_naturals())

def gen_str_palindrome():
    """
    Generates all ASCII alphanumeric palindromic strings
    """
    # nested functions yay!
    for s in gen_str():
        result_string = ''.join(s) # :(
        yield result_string[:0:-1] + result_string
        yield result_string[::-1] + result_string

def binary_palindromes():
    mid = ['', '0', '1']
    r = 0
    while True:
        for p in itertools.product('01', repeat=r):
            s = ''.join(p)
            for m in mid:
                yield s + m + s[::-1]
        r += 1

if __name__ == "__main__":
    i = 0
    #for x in all_chars():
    #for x in gen_str():
    #for x in gen_str_palindrom():
        #print "%5d\t%s" % (i, ''.join(x))
        #i += 1
        #if i >= 1000:
            #break
    for x in gen_str_palindrome():
        print x
        i += 1
        if i > 300: break
